import cd from '/api/cartData.js';
import hs from '/components/head-search.js';

render();

/* 生成右侧工具条购物车内容 */
function render(cart) {
    cd.getCart(data => {
        // console.log(data);
        let str = ``;
        if (data == 'noData' || data == 'err') {
            $('.tab-sub').text(0).hide();
            $('.bar-cart-list').html('').hide().siblings().find('.bar-cart-none').show().siblings().find('.bct-count').text(0);
            $('.bar-cart-none').show();
            return;
        }
        $('.bar-cart-list').show().siblings().find('.bar-cart-none').hide();
        $('.bar-cart-none').hide();
        const usrData = JSON.parse(localStorage.getItem('usrData'));
        if (usrData) {
            $('.usrid').text(usrData.usr);
        }
        $('.bar-cart-list').html(data.map(d => {
            const count = d[0];
            const p = d[1];
            return `
            <li>
                <a href="/pages/detial.html?pid=${p.pid}">
                    <img src=${p.imgsrc}>
                </a>
                <div>
                    <a href="/pages/detial.html?pid=${p.pid}">${p.name}</a>
                    <span>
                        <p><strong data-total=${p.price*count}>${p.price}</strong>x${count}</p>
                    </span>
                </div>
                <a href="javascript:;" data-id=${p.pid} class="bar-cart-del">删除</a>
            </li>
            `;
        }));
        /* 设置数量 */
        $('.bct-count').text(data.length);
        $('.tab-sub').text(data.length).show();
        /* 设置总价 */
        setBarCartTotal();
        /* 添加删除功能 */
        $('.bar-cart-del').off('click').on('click', function() {
            cd.del($(this).data('id'), (count) => {
                console.log(count);
                $('.bct-count').text(count);
                setBarCartTotal();

                hs.render();
                render();
            })
        })
        $('.fixed-bar-wrap').on('click', function(e) {
            e.stopPropagation()
            render();
            $('.fixed-bar').css('right', 270);
        });
        $(document).on('click', function(e) {
            $('.fixed-bar').css('right', 0);
        })
    })
}

/* 设置购物车面板总数 */
function setBarCartTotal() {
    console.log('test');
    let total = 0;
    $('.bar-cart-list li').each((index, el) => {
        // console.log(+($(el).find('strong').data('total')));
        total += +($(el).find('strong').data('total'));
    })
    $('.bct-total').text(parseInt(total * 100) / 100);
}
/* 加入购物车效果 */
function intoCart(o, e) {
    // $('.tab-cart').off
    const obj = $(o).clone().appendTo('body').css({
        left: e.left,
        top: e.top
    }).addClass('fly-item');
    const [
        sL,
        sT
    ] = [e.left, e.top];
    const [
        eL,
        eT
    ] = [$('.tab-cart').offset().left, $('.tab-cart').offset().top];
    const a = 0.0005;

    const b = (sT - a * sL ** 2 - eT + a * eL ** 2) / (sL - eL)

    const c = sT - a * sL ** 2 - b * sL

    let x = sL;
    let timer = setInterval(function() {
        x += 30;
        const y = a * x ** 2 + b * x + c
        $(obj).offset({
            left: x,
            top: y
        });
        if (x >= eL) {
            clearInterval(timer)
            $(obj).remove();
            /* 刷新侧边栏购物车 */
            render();
            /* 刷新搜索栏购物车 */
            hs.render();
            // setBarCartTotal();
        }
    }, 20)
}
export default {
    render,
    intoCart
}